<template>
  <div>
    <el-card style="margin-bottom:20px">
      <el-button type="primary" @click="handleAddGoods">新增商品</el-button>
    </el-card>
    <el-card>
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="商品名称" width="180">
        </el-table-column>
        <el-table-column label="商品图片" width="180" align="center">
          <template slot-scope="scope">
            <img
              width="100"
              height="100"
              :src="scope.row.pic || errorImg"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column label="商品价格" width="180" align="center">
          <template slot-scope="scope">
            <div>现价: {{ scope.row.price }}</div>
            <div>原价: {{ scope.row.originalPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column label="商品类别" width="" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.productCategoryName || '无' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="商品标签" width="" align="center">
          <template slot-scope="scope">
            <div>
              最新:
              <el-switch
                v-model="scope.row.newStatus"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </div>

            <div>
              推荐:
              <el-switch
                v-model="scope.row.previewStatus"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small">编辑</el-button>
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              @confirm="handleDeleteGoods(scope.row.id)"
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
            >
              <el-button type="text" size="small" slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getGoodsListApi, deleteGoods } from '../../api/request'
export default {
  data() {
    return {
      goodsList: [],
      switchNews: true,
      switchHots: true,
      errorImg: 'https://pic.imgdb.cn/item/6072d6208322e6675cbdc590.png',
      pages: {
        start: 1,
        limit: 10,
      },
    }
  },
  computed: {},
  created() {
    this.getGoodsList(this.pages)
  },
  methods: {
    // 获取商品列表
    getGoodsList(data) {
      // 请求接口
      getGoodsListApi(data).then((res) => {
        const { rows: goodsList } = res.data
        // 为switch更改属性
        goodsList.forEach((ele) => {
          ele.newStatus == 1 ? (ele.newStatus = true) : (ele.newStatus = false)
          ele.previewStatus == 1
            ? (ele.previewStatus = true)
            : (ele.previewStatus = false)
        })
        this.goodsList = goodsList
      })
    },
    // 删除商品
    handleDeleteGoods(id) {
      console.log(id)
      deleteGoods(id).then((res) => {
        // 删除成功 刷新列表数据
        console.log(res)
        if (res.code == 20000) {
          this.getGoodsList(this.pages)
          this.$message.success('删除成功')
        } else this.$message.error('删除失败')
      })
    },
    // 新增商品
    handleAddGoods() {
      this.$router.push('/goodsadd')
    },
  },
}
</script>

<style scoped lang="scss"></style>
